// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// b [author] rhyme_lph
// s [email]  rhymelph@gmail.com
// A [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:dio/src/response.dart';
import 'package:finance_app/src/entities/bill_entity.dart';
import 'package:finance_app/src/entities/business_contact_entity.dart';
import 'package:finance_app/src/entities/transaction_entity.dart';
import 'package:finance_app/src/http_service/http_bill_service.dart';
import 'package:finance_app/src/http_service/http_transaction_service.dart';
import 'package:finance_app/src/mixin/filter_transaction_sort_mixin.dart';
import 'package:finance_app/src/mixin/format_group_map_mixin.dart';
import 'package:finance_app/src/mixin/refresh_mixin.dart';
import 'package:finance_app/src/mixin/search_data_list_mixin.dart';
import 'package:finance_app/src/mixin/time_sort_mixin.dart';
import 'package:finance_app/src/mixin/transaction_data_mixin.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/utils/reg_exp_utils.dart';

class AddPaymentProvider extends BaseProvider
    with
        SearchDataListMixin,
        TimeSortMixin,
        FilterTransactionSortMixin,
        TransactionDataMixin,
        RefreshMixin<dynamic, List<TransactionEntity>> {
  List<TransactionEntity> transactions = [];

  void onCancel() {
    transactions.clear();
    transformData(data, false);
    update();
  }

  void onAdd() {
    if (transactions.length > 0) {
      double total = 0.0;
      transactions.forEach((element) {
        total += element.total.toDouble();
      });
      print(total);
      if (transactions.first.transactionType == 'bill') {
        Routes.navigateTo(RouterGenProviders.newCashOutPage,
            params: {
              'billList': transactions
                  .map((e) => BillEntity()
                    ..id = e.transactionableId
                    ..number = e.number
                    ..contact = (BusinessContactEntity()
                      ..id = e.contactId
                      ..name = e.name))
                  .toList(),
              'total': total,
            },
            replace: true);
      } else {
        final e = transactions.first;
        Routes.navigateTo(RouterGenProviders.newCashOutPage,
            params: {
              // 'billList': transactions,
              // 'total': total,
              'paidTo': BusinessContactEntity()
                ..id = e.contactId
                ..name = e.name,
              'total': e.total.toDouble(),
              'type': 'expense',
              'expenseId': e.transactionableId,
            },
            replace: true);
      }
    }
  }

  void onSelect(TransactionEntity item1) {
    if (transactions.contains(item1)) {
      transactions.remove(item1);
    } else {
      transactions.add(item1);
    }
    formatMap(transactionList, true);
    update();
  }

  bool handleFilter(TransactionEntity a) {
    bool listFilter = false;
    if (transactions.length == 0) {
      listFilter =
          RegExpUtils.getSearchRegExp(searchController.text).hasMatch(a.name);
    } else if (transactions.length > 0) {
      final first = transactions.first;
      listFilter = first.contactId == a.contactId &&
          first.transactionType == a.transactionType;
    }
    return listFilter && a.status == 'open';
  }

  @override
  List<TransactionEntity> get dataList => transactionList;

  @override
  List<String> get types => ['unpaid_out'];


  @override
  Future get dataFuture => null;
}
